/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.mesh;

import technotheist.geo.map.MapUtil;

/**
 *
 * @author Mitchel Strachan
 */
public class PlanetGeom {

    private final PlanetPatch[][][] patchGrid;
    private final float step;
    private final int size;


    PlanetGeom(int size) {
        this.size = size;
        this.step = 1.0f / size;
        this.patchGrid = new PlanetPatch[MapUtil.PANELS][size][size];
        init();
    }
    
    
    private void init() {

        for (byte p = 0; p < MapUtil.PANELS; p++) {
            for (int u = 0; u < size; u++) {
                for (int v = 0; v < size; v++) {
                    patchGrid[p][u][v] = new PlanetPatch(this, p, u * step, v * step, step);
                }
            }
        }

        for (byte p = 0; p < MapUtil.PANELS; p++) {
            for (int u = 0; u < size; u++) {
                for (int v = 0; v < size; v++) {
                    if (v > 0) {
                        patchGrid[p][u][v].sw = patchGrid[p][u][v - 1];
                    } else {
                        if ((p & 1) == 0) {
                            patchGrid[p][u][v].sw = patchGrid[(p + MapUtil.P_SUB1) % MapUtil.PANELS][u][size - 1];
                        } else {
                            patchGrid[p][u][v].sw = patchGrid[(p + MapUtil.P_SUB2) % MapUtil.PANELS][size - 1][size - 1 - u];
                        }
                    }
                    if (u > 0) {
                        patchGrid[p][u][v].nw = patchGrid[p][u - 1][v];
                    } else {
                        if ((p & 1) == 0) {
                            patchGrid[p][u][v].nw = patchGrid[(p + MapUtil.P_SUB2) % MapUtil.PANELS][size - 1][v];
                        } else {
                            patchGrid[p][u][v].nw = patchGrid[(p + MapUtil.P_SUB1) % MapUtil.PANELS][size - 1 - v][size - 1];
                        }
                    }
                    if (v < size - 1) {
                        patchGrid[p][u][v].ne = patchGrid[p][u][v + 1];
                    } else {
                        if ((p & 1) == 0) {
                            patchGrid[p][u][v].ne = patchGrid[(p + 2) % MapUtil.PANELS][0][size - 1 - u];
                        } else {
                            patchGrid[p][u][v].ne = patchGrid[(p + 1) % MapUtil.PANELS][u][0];
                        }
                    }
                    if (u < size - 1) {
                        patchGrid[p][u][v].se = patchGrid[p][u + 1][v];
                    } else {
                        if ((p & 1) == 0) {
                            patchGrid[p][u][v].se = patchGrid[(p + 1) % MapUtil.PANELS][0][v];
                        } else {
                            patchGrid[p][u][v].se = patchGrid[(p + 2) % MapUtil.PANELS][size - 1 - v][0];
                        }
                    }
                }
            }
        }
    }
}
